package com.feibi.study.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.feibi.study.Adapter.MenuAdapter;
import com.feibi.study.Interface.OnItemClickListener;
import com.feibi.study.R;

import java.util.ArrayList;

public class MenuDialog {
    private Context context;
    private Dialog dialog;
    private Display display;
    private TextView tv_cancel;
    private RecyclerView rv_menus;

    public MenuDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        builder();
    }

    private MenuDialog builder() {
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_bottom_menu, null);
        rv_menus = view.findViewById(R.id.rv_menus);
        tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(v -> dismiss());
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        view.setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (display.getWidth());
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(lp);
        dialogWindow.setWindowAnimations(R.style.dialog_anim);
        return this;
    }

    /**
     * 设置選擇項目
     *
     * @param menus
     * @return
     */
    public MenuDialog setFilter(ArrayList<String> menus, ArrayList<Integer> menuIcons, final OnItemClickListener listener) {
        MenuAdapter menuAdapter = new MenuAdapter(context, menus, listener);
        menuAdapter.setMenuIcons(menuIcons);
        rv_menus.setLayoutManager(new LinearLayoutManager(context));
        rv_menus.setAdapter(menuAdapter);
        return this;
    }

    public void show() {
        dialog.show();
    }

    public boolean isShowing() {
        if (dialog != null) {
            if (dialog.isShowing())
                return true;
            else
                return false;
        }
        return false;
    }

    public void dismiss() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }
}
